<?php

namespace App\Services\GiftVipPurchase;

use App\Models\User;
use Carbon\Carbon;
use App\Models\GoldRound;
use App\Models\GoldProduct;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class GiftVipPurchaseRequest extends FormRequest
{
	public function rules()
	{
		return [
			'guid' => [
				'required',
				'integer',
				'min:1',
				$this->inviteCode(),
			],
			'cards' => [
				'required',
				'integer',
				'min:1',
				$this->vipNumbers(),
			],
		];
	}

	public function inviteCode()
	{
		return function ($key, $value, $fail) {

			/**
			 * @var User $user
			 */
			$user = $this->user();
			$child = User::find($value);

			if (! $child) {
				return $fail('目标用户不存在');
			} elseif ($child->id == $user->id) {
				return $fail('仅可向他人转出');
			} else {
				$this->child = $child;
				return true;
			}
		};
	}

	public function vipNumbers()
	{
		return function ($key, $value, $fail) {

			$user = $this->user();

			if (big_decimal($user->vip_cards)->isLessThan($value)) {
				return $fail('用户VIP卡不足');
			} 

			return true;
		};
	}

	public function getGuser()
	{
		return $this->child;
	}

}
